package cn.anc.aonicardv.util;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import cn.anc.aonicardv.bean.PhotoBean;
import cn.anc.aonicardv.bean.VideoBean;
import cn.anc.httpcontrolutil.CarControl;

/**
 * Created by yangdai on 2017/5/24.
 */

public class AoniBeanParseUtils {

    public static List<PhotoBean> getPhotoListByPhotoMediaInfo(String msg) {
        List<PhotoBean> photoBeanList = new ArrayList<>();
        try {
            JSONArray array = new JSONArray(msg);
            for (int i = 0; i < array.length(); i++) {
                JSONObject obj = array.getJSONObject(i);
                PhotoBean photoBean = new PhotoBean();
                if (obj.has("path")) {
                    photoBean.setPath(obj.getString("path"));
                }
                int start = photoBean.getPath().lastIndexOf("/");
                int end = photoBean.getPath().lastIndexOf(".");
                Date nameDate = DateUtils.parseDate(photoBean.getPath().substring(start + 1, end).replace("_", "").substring(0, 14), DateUtils.DF_YYYYMMDDHHMMSS);
                String filename = DateUtils.formatDateTime(nameDate, DateUtils.DF_YYYY_MM_DD_HH_MM_SS);
                String date = obj.getString("date");
                photoBean.setName(filename);
                photoBean.setDate(date);
                photoBean.setItemPosition(i);
                photoBeanList.add(photoBean);
            }
            if (CarControl.DeviceInfo.chip.equals("LT"))
                photoBeanList = getLTPhotoList(photoBeanList);
            if (CarControl.DeviceInfo.chip.equals("MSTAR"))
                Collections.reverse(photoBeanList);
            return photoBeanList;
        } catch (Exception ex) {
            return photoBeanList;
        }
    }

    private static List<PhotoBean> getLTPhotoList(List<PhotoBean> photoBeanList) {
        // 排序
        // 自然排序
        // Collections.sort(list);
        // 比较器排序
        // 如果同时有自然排序和比较器排序，以比较器排序为主
        Collections.sort(photoBeanList, new Comparator<PhotoBean>() {
            @Override
            public int compare(PhotoBean v1, PhotoBean v2) {
                int num = v1.getDate().compareTo(v2.getDate());//先比较时间
                if (num == 0) {
                    return v1.getName().compareTo(v1.getName());//如果时间相同，再比较名字（名字按Unicode编码升序排序）
                }
                return num;

            }

        });
        Collections.reverse(photoBeanList); // 倒序排列
        return photoBeanList;

    }


    public static List<VideoBean> getVideoListByVideoMediaInfo(final String msg) {
        LogUtil.e("AoniBeanParseUtils","----------getVideoListByVideoMediaInfo----------:"+msg);
        List<VideoBean> videoList = new ArrayList<>();
        try {
            JSONArray array = new JSONArray(msg);
            for (int i = 0; i < array.length(); i++) {
                JSONObject obj = array.getJSONObject(i);
                VideoBean videoBean = new VideoBean();
                if (obj.has("path")) {
                    videoBean.setPath(obj.getString("path"));
                }
                int start = videoBean.getPath().lastIndexOf("/");
                int end = videoBean.getPath().lastIndexOf(".");
                String date = obj.getString("date");
                Date nameDate = DateUtils.parseDate(videoBean.getPath().substring(start + 1, end).replace("_", "").substring(0, 14), DateUtils.DF_YYYYMMDDHHMMSS);
                String filename = DateUtils.formatDateTime(nameDate, DateUtils.DF_YYYY_MM_DD_HH_MM_SS);
                videoBean.setDate(date);
                videoBean.setName(filename);
                videoBean.setItemPosition(i);
                videoList.add(videoBean);
            }
            if (CarControl.DeviceInfo.chip.equals("LT"))
                videoList = getLTVideoList(videoList);
            if (CarControl.DeviceInfo.chip.equals("MSTAR"))
                Collections.reverse(videoList);
            LogUtil.e("AoniBeanParseUtils","--------------videoList.---:"+videoList.size());

            return videoList;
        } catch (Exception ex) {
            LogUtil.e("AoniBeanParseUtils","-----------------Exception---:"+ex.getMessage());
            return videoList;
        }

    }

    private static List<VideoBean> getLTVideoList(List<VideoBean> videoBeanList) {
        // 排序
        // 自然排序
        // Collections.sort(list);
        // 比较器排序
        // 如果同时有自然排序和比较器排序，以比较器排序为主
        Collections.sort(videoBeanList, new Comparator<VideoBean>() {
            @Override
            public int compare(VideoBean v1, VideoBean v2) {

                int num = v1.getDate().compareTo(v2.getDate());//先比较时间
                if (num == 0) {
                    return v1.getName().compareTo(v1.getName());//如果时间相同，再比较名字（姓名按Unicode编码升序排序）
                }
                return num;

            }

        });
        Collections.reverse(videoBeanList); // 倒序排列
        return videoBeanList;

    }
}
